define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/agent/index',
                    add_url: 'user/agent/add',
                    edit_url: 'user/agent/edit',
                    del_url: 'user/agent/del',
                    multi_url: 'user/agent/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        // {field: 'group.name', title: __('Group')},
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'lev.name', title: __('分销等级'), operate: 'LIKE' },

                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'code', title: __('邀请码'), operate: 'LIKE' },

                        { field: 'commission', title: __('可用佣金'), operate: false, sortable: true },
                        { field: 'contact', title: __('Contact'), operate: 'LIKE' },
                        { field: 'contact_phone', title: __('Contact_phone'), operate: 'LIKE' },

                        // {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        { field: 'id_card', title: __('身份证'), operate: 'LIKE' },
                        { field: 'address', title: __('通讯地址'), operate: 'LIKE' },
                        { field: 'agent_area', title: __('服务商区域'), operate: false },
                        { field: 'agent_rate', title: __('服务商佣金比例%'), operate: false },

                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        { field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [


                                {
                                    name: 'commission', //唯一标识、权限标识
                                    dropdown: '', //按钮下拉分组，默认为不分组
                                    text: '佣金记录', //按钮显示的文字，支持function
                                    title: '佣金记录', //按钮显示的文字，支持function
                                    classname: 'btn btn-info btn-xs btn-dialog', //按钮的class，支持btn-dialog/btn-ajax/btn-addtabs
                                    // icon: 'fa fa-plus', //按钮的图标
                                    url: 'user/agent/commission/user_id/{ids}', //按钮的链接，支持使用{字段名}来占位替换，支持`function`
                                    // confirm: '是否同意该请求？', //点击按钮后的确认框，支持`function`
                                    refresh: true, //操作完成后是否刷新列表
                                    // disable: function (row) { }, //判断按钮是否禁用
                                    visible: function (row) {
                                        let options = table.bootstrapTable('getOptions');
                                        console.log(options);
                                        if (options.operateCommission) {
                                            return true;
                                        } else {
                                            return false
                                        }

                                    }, //判断按钮是否可见
                                    // hidden: function (row) { }, //判断按钮是否隐藏
                                    extend: '', //扩展的扩展属性
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        commission: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/agent/commission' + location.search,
                    // add_url: 'user/commission/add',
                    // edit_url: 'user/commission/edit',
                    // del_url: 'user/commission/del',
                    // multi_url: 'user/commission/multi',
                    // import_url: 'user/commission/import',
                    table: 'user_commission',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'user_id', title: __('User_id'), visible: false },
                        { field: 'user.nickname', title: __('昵称'), operate: 'LIKE' },
                        { field: 'order_no', title: __('订单编号'), operate: 'LIKE' },
                        { field: 'money', title: __('金额'), operate: 'BETWEEN' },
                        { field: 'msg', title: __('备注'), operate: 'LIKE' },
                        // {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // {field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});